﻿using System;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class AddNewUserScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserRoles();
                txtExpiryDate.Enabled = false;
                MaskedEditValidator1.Enabled = false;

                BindUsers();
            }
        }

        private void BindUserRoles()
        {
            //TODO: Bind the roles to combobox.
            ddlUserRoles.DataSource = EntityFactory.GetUserRoles();
            ddlUserRoles.DataBind();
        }

        protected void OnBtnCreateClicked(object sender, EventArgs e)
        {
            string _password = CommonMethods.CreateRandomPassword(6);

            if (string.IsNullOrEmpty(txtName.Text) == true)
            {
                MsgBox1.alert("Please enter the name");
                return;
            }
            if (string.IsNullOrEmpty(txtSurname.Text) == true)
            {
                MsgBox1.alert("Please enter the surname");
                return;
            }
            if (string.IsNullOrEmpty(txtUserName.Text) == true)
            {
                MsgBox1.alert("Please enter the username");
                return;
            }

            if (((UserType)ddlUserRoles.SelectedIndex) == UserType.Student)
            {
                bool _successfull;
                DateTime _expiryDate = CommonMethods.ToDateTime(txtExpiryDate.Text, "dd.MM.yyyy", out _successfull);

                if (_successfull == false)
                {
                    return;
                }

                ((Instructor)EntityFactory.User).AddNewUser(txtName.Text, txtSurname.Text, txtUserName.Text,
                                                             _password,
                                                             txtEMail.Text, (UserType)ddlUserRoles.SelectedIndex,
                                                             _expiryDate);
            }
            else
            {
                    ((Instructor)EntityFactory.User).AddNewUser(txtName.Text, txtSurname.Text, txtUserName.Text,
                                                                         _password,
                                                                         txtEMail.Text, (UserType)ddlUserRoles.SelectedIndex,
                                                                         DateTime.Now);
                
            }

            CommonMethods.NotifyNewUser(txtUserName.Text, _password, txtEMail.Text, ddlUserRoles.SelectedItem.Text);

            MsgBox1.alert("User is created!");

            txtUserName.Text = string.Empty;
            txtSurname.Text = string.Empty;
            txtName.Text = string.Empty;
            txtEMail.Text = string.Empty;
        }

        protected void OnDdlUserRolesSelectedIndexChanged(object sender, EventArgs e)
        {
            txtExpiryDate.Enabled = ddlUserRoles.Items.FindByText("Student").Selected == true;
            MaskedEditValidator1.Enabled = ddlUserRoles.Items.FindByText("Student").Selected == true;
            if (txtExpiryDate.Enabled == true)
            {
                txtExpiryDate.Focus();
            }
        }

        private void BindUsers()
        {
            dtgUsers.DataSource = EntityFactory.GetAllUsers();
            dtgUsers.DataBind();
        }

        protected void OnDtgUsersRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int _userId = CommonMethods.GetIdFromGridViewRow(dtgUsers.Rows[e.RowIndex]);

            UsersTableAdapter _usersTableAdapter = new UsersTableAdapter();
            _usersTableAdapter.DeleteUser(_userId);

            BindUsers();
        }
    }
}